﻿@model ElectroSystem.Models.LogEntryModel   

@{
    ViewBag.Title = "Access Log Entries";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Access Log Entries</h2>

<script type="text/javascript">
    $(function () {
        $("#startdt").datepicker();
        $("#enddt").datepicker();
    });

    function sortbydate() {
        $.ajax({
            url: '@Url.Action("SearchByDateDESC")',
            type: "GET",
            datatype: "html",
            UpdateTargetId: "divResult", 
            data: null, 
            error: function (xmlHttpRequest, errorText, thrownError) {
                alert("An error has occurred.");
            },
            success: function (data) {
                $('#divResult').empty();
                $('#divResult').html(data);
            }
        });
    }

    function searchusername() {
        if ($("#ddlUsers").val() == "") {
            alert("Choose a Username to access its log entries");
        }
        else {
            var client = $('#ddlUsers').val();
            var param = { username: client };

            $.ajax({
                url: '@Url.Action("SearchByClientID")',
                type: "GET",
                datatype: "html",
                UpdateTargetId: "divResult", // The DIV that will contain the partial view
                data: param, // Passing the parameters var
                error: function (xmlHttpRequest, errorText, thrownError) {
                    alert("An error has occurred.");
                },
                success: function (data) {
                    $('#divResult').empty();
                    $('#divResult').html(data);
                }
            });
        }
    }

    function searchfaultid() {
        if ($("#ddlFaults").val() == "") {
            alert("Choose a fault to access its log entries");
        }
        else {
            var fault = $('#ddlFaults').val();
            var param = { faultID: fault };
        $.ajax({
            url: '@Url.Action("SearchByFaultID")',
            type: "GET",
            datatype: "html",
            UpdateTargetId: "divResult", // The DIV that will contain the partial view
            data: param, // Passing the parameters var
            error: function (xmlHttpRequest, errorText, thrownError) {
                alert("An error has occurred.");
            },
            success: function (data) {
                $('#divResult').empty();
                $('#divResult').html(data);
            }
        });
      }
    }

    function searchdate() {
        if (($("#startdt").val() == "") && ($("#enddt").val() == "")) {
            alert("Choose a start date and end date to access the log entries");
        }
        else if ($("#startdt").val() == "") {
            alert("Choose a start date to access the log entries");
        }
        else if ($("#enddt").val() == "") {
            alert("Choose an end date to access the log entries");
        }
        else {
            var startdate = $('#startdt').val();
            var enddate = $('#enddt').val();

            var param = { startDate: startdate, endDate: enddate };

            $.ajax({
                url: '@Url.Action("SearchByLogDate")',
                type: "GET",
                datatype: "html",
                UpdateTargetId: "divResult", // The DIV that will contain the partial view
                data: param, // Passing the parameters var
                error: function (xmlHttpRequest, errorText, thrownError) {
                    alert("An error has occurred.");
                },
                success: function (data) {
                    $('#divResult').empty();
                    $('#divResult').html(data);
                }
            });
        }
    }

    function searchall() {
        if (($("#startdt").val() == "") && ($("#enddt").val() == "") && ($("#ddlFaults").val() == "") && ($("#ddlUsers").val() == "")) {
            alert("Choose fault id, username, start date and enter date to access log entries");
        }
        else if ($("#ddlFaults").val() == "") {
            alert("Choose a fault ID to access the log entries");
        }
        else if ($("#ddlUsers").val() == "") {
            alert("Choose a Username to access the log entries");
        }
        else if ($("#startdt").val() == "") {
            alert("Choose a start date to access the log entries");
        }
        else if ($("#enddt").val() == "") {
            alert("Choose an end date to access the log entries");
        }
        else {
            var fault = $('#ddlFaults').val();
            var client = $('#ddlUsers').val();
            var startdate = $('#startdt').val();
            var enddate = $('#enddt').val();

            var param = { faultID: fault, username: client, startDate: startdate, endDate: enddate };

            $.ajax({
                url: '@Url.Action("SearchByAllLogs")',
                type: "GET",
                datatype: "html",
                UpdateTargetId: "divResult", // The DIV that will contain the partial view
                data: param, // Passing the parameters var
                error: function (xmlHttpRequest, errorText, thrownError) {
                    alert("An error has occurred.");
                },
                success: function (data) {
                    $('#divResult').empty();
                    $('#divResult').html(data);
                }
            });
        }
    }
</script>

<button type="button" onclick="sortbydate()">Sort by Date</button>
<br /> <br />

@Html.Label("Username: ") <br />
@Html.DropDownListFor(model => Model.Users, Model.Users, "--Select Username--", new { id = "ddlUsers" })
<button type="button" onclick="searchusername()">Search by Username</button>
<br /> <br />

@Html.Label("Fault ID: ") <br />
@Html.DropDownListFor(model => model.FaultID, Model.Faults, "--Select FaultID--", new { id = "ddlFaults" })
<button type="button" onclick="searchfaultid()">Search by FaultID</button>
<br /> <br />

@Html.Label("Start Date: ") 
<input type="text" id="startdt" /> <br />
@Html.Label("End Date: ") 
<input type="text" id="enddt" /> <br />
<button type="button" onclick="searchdate()">Search by Date</button>
<br /> <br />

<button type="button" onclick="searchall()">Search by All</button>
<br /> <br />

<div id = "divResult">
</div>


